package com.lemner.www.hongyu.Activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.lemner.www.hongyu.Bean.Userbean;
import com.lemner.www.hongyu.R;
import com.lemner.www.hongyu.Service.Service;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ReviseActivity extends AppCompatActivity implements View.OnClickListener {
    private EditText et_oldpwd,et_newpwd,et_new2pwd;
    private Button btn_modify;
    private Userbean.DataBean date;
    private static String BASEURL="http://www.hongyudasha.com/propertysystem/";
    private Service service;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_revise);
        InitView();
        date= (Userbean.DataBean)getIntent().getSerializableExtra("date");
    }
    private void InitView(){
        et_oldpwd=(EditText)findViewById(R.id.et_oldpwd);
        et_newpwd=(EditText)findViewById(R.id.et_newpwd);
        et_new2pwd=(EditText)findViewById(R.id.et_new2pwd);
        btn_modify=(Button)findViewById(R.id.btn_modify);
        btn_modify.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
      switch (v.getId()){
          case R.id.btn_modify:
              if(TextUtils.isEmpty(et_oldpwd.getText().toString())
                      ||TextUtils.isEmpty(et_newpwd.getText().toString())
                      ||TextUtils.isEmpty(et_new2pwd.getText().toString())) {
                  Toast.makeText(this, "请输入要修改的密码", Toast.LENGTH_SHORT).show();
              }
              else if(et_newpwd.getText().toString().equals(et_new2pwd.getText().toString())){
                  getDate();
                  Toast.makeText(getApplication(),"修改成功",Toast.LENGTH_SHORT).show();
                  Intent intent = new Intent(ReviseActivity.this, LoginActivity.class);
                  startActivity(intent);
              }
              else {
                  Toast.makeText(this, "有误", Toast.LENGTH_SHORT).show();
              }
              break;
          default:
              break;
      }
    }

    public static String md5(String string) {
        if (TextUtils.isEmpty(string)) {
            return "";
        }
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
            byte[] bytes = md5.digest(string.getBytes());
            String result = "";
            for (byte b : bytes) {
                String temp = Integer.toHexString(b & 0xff);
                if (temp.length() == 1) {
                    temp = "0" + temp;
                }
                result += temp;
            }
            return result;
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return "";
    }

    public void getDate(){
        int id=date.getUser().getWorker_id();
        String pwd=et_new2pwd.getText().toString();
         pwd=md5(pwd);
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
     service=retrofit.create(Service.class);
        Call<Userbean> call=service.getRevise(id,pwd);
        call.enqueue(new Callback<Userbean>(){
            @Override
            public void onResponse(Call<Userbean> call, Response<Userbean> response) {

            }

            @Override
            public void onFailure(Call<Userbean> call, Throwable t) {
                Log.e("error", t.getMessage());
            }
        });

    }

}
